<template>
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" :size="formSize" status-icon >
        <el-descriptions :column="2" border >
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">商品名称</div>
                </template>
                <el-form-item prop="title">
                    <el-input v-model="ruleForm.title" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">单位　　</div>
                </template>
                <el-form-item prop="unit">
                    <el-input v-model="ruleForm.unit" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">规格　　</div>
                </template>
                <el-form-item prop="specs">
                    <el-input v-model="ruleForm.specs" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">品牌　　</div>
                </template>
                <el-form-item prop="brand">
                    <el-input v-model="ruleForm.brand" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">金额　　</div>
                </template>
                <el-form-item prop="price">
                    <el-input v-model="ruleForm.price" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">数量　　</div>
                </template>
                <el-form-item prop="sums">
                    <el-input v-model="ruleForm.sums" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">类型　　</div>
                </template>
                <el-form-item prop="type">
                    <el-radio-group v-model="ruleForm.type">
                        <el-radio :value="1" size="large">材料</el-radio>
                        <el-radio :value="3" size="large">双包</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">备注　　</div>
                </template>
                <el-form-item prop="title">
                    <el-input v-model="ruleForm.remarks" />
                </el-form-item>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">附件　　</div>
                </template>
                <el-form-item prop="title">
                    <el-button type="primary" @click="onUpload" size="small">{{ ruleForm.files ? '查看' : '上传' }}</el-button>
                </el-form-item>
            </el-descriptions-item>
        </el-descriptions>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
        <el-button type="info" @click="router.go(-1)"> 返回 </el-button>
        <component :is="applyButton" model = "Material" source = "Material" type = "apply" :id = id />
        <component :is="applyRevoke" model = "Material" source = "Material" :id = id />
    </div>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="upload" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

</template>
  
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import http from '../../../util/request';
import router from '../../../router';
import applyButton from "@/components/applyButton.vue";
import applyRevoke from "@/components/applyRevoke.vue";
import upload from '@/components/upload.vue'

const loading = ref<boolean>(false)
const type:any = ref(useRouter().currentRoute.value.params.type)
const id:any = ref(useRouter().currentRoute.value.params.id)
const showUpload = ref<boolean>(false)
let uploadData:any = ref({prefix:'labor/'})

interface RuleForm {
    id:number
    project_id: number
    user_id: number
    title:string
    price: number
    specs: string
    unit: string
    sums: number
    brand: string
    remarks: string
    type: number
    files: string
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id:0,
    title:'',
    project_id: 0,
    user_id: 0,
    price: 0,
    specs: '/',
    unit: '/',
    sums: 0,
    brand: '/',
    remarks: '/',
    type: 1,
    files:'',
})


const getData = () => {
    if(id.value == 0){
        return;
    }
    http.get('/material/'+id.value).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/material',ruleForm.value)
            }else{
                request = http.put('/material',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                id.value = response.data.data.id
                getData()
                // if(type.value == 2){
                //     router.push({ name: 'materialSmall'})
                //     return
                // }
                // if(type.value == 3){
                //     router.push({ name: 'materialScene'})
                //     return
                // }
                // router.push({ name: 'material'})
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onUpload = () => {
    uploadData.value.id = id.value
    uploadData.value.image = ruleForm.value.files
    uploadData.value.ratio = ''
    showUpload.value = true
}

const saveUpload = (data:string) => {
    ruleForm.value.files = data
    showUpload.value = false
}

onMounted(()=>{
    ruleForm.value.type = parseInt(type.value)
    getData()
})

</script>
<style scoped>
.el-form{margin:0px auto;width: 1200px;height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
.cell-item{font-size: 16px;}
.el-form-item{margin-top: 0px;margin-bottom: 8px;}
::v-deep .el-tabs__item{font-size: 18px !important;}
</style>
